/*
package main

import (
	"fmt"
	"image"
	"image/color"
	"image/png"
	"log"
	"os"
)

func main() {
	const (
		dx = 300
		dy = 500
	)

	// 需要保存的文件
	imgcounter := 123
	imgfile, _ := os.Create(fmt.Sprintf("%03d.png", imgcounter))
	defer imgfile.Close()

	// 新建一个 指定大小的 RGBA位图
	img := image.NewNRGBA(image.Rect(0, 0, dx, dy))

	for y := 0; y < dy; y++ {
		for x := 0; x < dx; x++ {

			if x%8 == 0 {
				// 设置某个点的颜色，依次是 RGBA
				img.Set(x, y, color.RGBA{uint8(x % 256), uint8(y % 256), 0, 255})
			} else {
				img.Set(x, y, color.White)
			}
		}
	}

	// 以PNG格式保存文件
	err := png.Encode(imgfile, img)
	if err != nil {
		log.Fatal(err)
	}

}
*/

package main

import (
	"fmt"
	"image"
	"image/draw"
	//	"image/color"
	//	"image/draw"
	"image/jpeg"
	"os"
)

func main() {
	f1, err := os.Open("1.jpg")
	if err != nil {
		panic(err)
	}
	defer f1.Close()
	f2, err := os.Open("2.jpg")
	if err != nil {
		panic(err)
	}
	defer f2.Close()
	f3, err := os.Create("3.jpg")
	if err != nil {
		panic(err)
	}
	defer f3.Close()

	m1, err := jpeg.Decode(f1)
	if err != nil {
		panic(err)
	}
	bounds := m1.Bounds()
	m2, err := jpeg.Decode(f2)
	if err != nil {
		panic(err)
	}

	m := image.NewNRGBA(image.Rect(0, 0, bounds.Dx()*2, bounds.Dy())) //2个f1宽度，高度相同

	draw.Draw(m, bounds, m1, image.ZP, draw.Src)                                                 //在（0，0）位置开始写入m1
	draw.Draw(m, image.Rect(bounds.Dx(), 0, bounds.Dx()*2, bounds.Dy()), m1, image.ZP, draw.Src) //再右边继续添加
	draw.Draw(m, m2.Bounds(), m2, image.ZP, draw.Src)                                            //左上角添加f2
	err = jpeg.Encode(f3, m, nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("ok\n")
}
